# -*- coding: utf-8 -*-
#!/usr/local/bin/python
# Author: yuxiangyu
# Date: Feb 11, 2022 2:48 PM
# =============================

'''
多数据库路由配置
'''
class DatabaseRouter:
    # {"app_label_name": "database_conn_name"}
    route_app_labels = {"data": "data", "user_info": "user"}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels.keys():
            return self.route_app_labels[model._meta.app_label]
        return 'default'

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels.keys():
            return self.route_app_labels[model._meta.app_label]
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        '''
        遗留数据库中的表不允许迁移
        '''
        if app_label in self.route_app_labels.keys():
            return False
        return True